package seg2500.messagerie.texte;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import com.lloseng.ocsf.client.*;
import java.util.*;
import seg2500.messagerie.*;

public class TexteClient implements Observer
{
  TexteFrame gui;

  public TexteClient(TexteFrame gui)
  {
	  this.gui= gui;
	  gui.setController(this);
  }

  public void send(String message)
  {
    try {

        MessageTextuel m= new MessageTextuel();
		m.setTexte(message);

		try
		{
			Session.instance().sendToServer(m);
		}
		catch (java.io.NotSerializableException ex)
		{

			System.out.println("Error! the object to be sent is not Serializable");
		} 
		catch (Exception ex)
		{

			System.out.println("Error! Can't send message" + ex);
		}

    }
    catch (Exception ex)
    {
		System.out.println("Probleme de communication..." +ex);
    }
  }

  public void update(Observable o, Object arg) {

	  if (arg instanceof MessageTextuel)
	  {

		  MessageTextuel m = (MessageTextuel)arg;
		  gui.display(m.getTexte());

	  }
	  else
	  {

		  System.out.println("Message recu" + arg.toString());
	  }
  }
}
